﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizShpScorePub : CustBizObject
    {
        private CDmShpScorePub m_CDmShpScorePub = new CDmShpScorePub();
        private CDmShpScorePub oDmShpScorePub
        {
            get { return m_CDmShpScorePub; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpScorePub()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpScorePub.SHP_SCORE_PUB);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["SCORE_PUB_ID"]; 
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            if (drw["PUB_NO"].ToString() == string.Empty)
            {
                drw["PUB_NO"] = msBsc.SvRef.ApplicationConst.Const.GetNewSerialNo("SCORE_PUB_NO", System.DateTime.Now.Year.ToString(), 4);
            }
            //drw["BUYER_DEL"] = false;
            //drw["BUYER_RATE_SID"] = "";
            if (drw["TOTAL_QTY"].ToString() == string.Empty)
            {
                drw["TOTAL_QTY"] = 0;
            }
            if (drw["TOTAL_AMT"].ToString() == string.Empty)
            {
                drw["TOTAL_AMT"] = 0;
            }
            if (drw["SCORE_PRICE"].ToString() == string.Empty)
            {
                drw["SCORE_PRICE"] = 0;
            }
            drw["SALES_QTY"] = 0;
            drw["SALES_AMT"] = 0;
            drw["DISCOUNT_AMT"] = 0;
            drw["STL_AMT"] = 0;
            drw["SURPLUS_QTY"] = drw["TOTAL_QTY"];
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            } 
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        { 
            return true;
        }

        public DataSet SelectByIds(string strIdS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('" + strIdS.Replace("'", "").Replace(",", "','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByPubNo(string strPubNo)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE '" + strPubNo + "'='' OR PUB_NO ='" + strPubNo + "' ORDER BY INITIAL_DT DESC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectSurplusQty()
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM SHP_SCORE_PUB A WHERE A.SURPLUS_QTY>0 AND A.SCORE_PUB_DT IS NOT NULL ORDER BY A.SCORE_PUB_DT ASC ");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

         
    }
}
